#include "aerial\Scout.h"

#include "aerial\Game\Equipment\Shield.h"
#include "aerial\Game\GameObject\Weapon\TracingMissile.h"
#include "raknet\BitStream.h"
#include "aerial\Game\Equipment\Propeller.h"

#include <iostream>

using namespace std;

const float Scout::SpeedPowerGen = 2.f;
const float Scout::MaxArmor = 100.f;
const float Scout::MaxPower = 50.f;
const float Scout::RadiusPS = 2.f;

const float Scout::MaxShieldRadius = 2.8f;
const float Scout::MPPS = 500.f;

Scout::Scout(int id, char* username, float x, float y, float A)
	: FlyObject(x, y, A, id, username)
	,burstType(0),countDt(0),countDt2(0),winType(0),type(0),count(0){
	power = MaxArmor;
	armor = MaxPower;

	b2CircleShape circle;
	circle.m_radius = 0.5f;

	b2FixtureDef fixtureDef;
	fixtureDef.shape = &circle;
	fixtureDef.density = 1.f;
	fixtureDef.friction = 1.f;
	fixtureDef.restitution = 1.f;

	body->CreateFixture(&fixtureDef);

	shield = new Shield(MaxShieldRadius, this);
	spr = hgeResMan->GetSprite("spr.scout");
}

Scout::~Scout() {	
}

void Scout::render()
{
	int m = status & S_M_WIN;
	if (m)
	{
		if(power>40)
		{
			winType = 1;
			power -=40;
		}
	}


	if ( winType )
	{
		countDt2++;
		if (countDt2 < 500)
			spr->SetColor(0x5fffffff);
		else
		{
			spr->SetColor(0xffffffff);
			countDt2 = 0;
			winType = 0;
		}
	}
	else
	{
		spr->SetColor(0xffffffff);
	}
	spr->RenderEx(T.x, T.y, A, 0.1f, 0.1f);
	shield->renderShield(T.x, T.y);
	propeller->rendPropeller();
	hgeResMan->GetFont("fnt.original")->printf(T.x, T.y, HGETEXT_LEFT, "%s", username);
}

bool Scout::update(float dt)
{
	// power regeneration
	power += SpeedPowerGen * dt;
	if (power > MaxPower) power = MaxPower;

	move(dt);	// action move
	turn(dt);	// action rotate

	// flyobject dealing with general equipment and network deviation
	FlyObject::update(dt);
	return false;
}

void Scout::move(float dt)
{
	int m = status & S_M_MOV;
	int n = status & S_M_BUR;
	if (n)
	{
		if ( power>35 )
		{
			burstType = 1;
			power -= 35;
		}
	}

	if ( burstType )
	{
		countDt++;
		if (countDt <400)
		{
			if (m) 
				SetSpeed(MPPS / BattleHelper::getRatio()*2);
			else SetSpeed(0.f);
		}
		else
		{
			if (m) 
				SetSpeed(MPPS / BattleHelper::getRatio());
			else SetSpeed(0.f);
			countDt = 0;
			burstType = 0;
		}
	}
	else
	{
		if (m) 
			SetSpeed(MPPS / BattleHelper::getRatio());
		else SetSpeed(0.f);
	}

}


void Scout::turn(float dt)
{
	int r = status & (S_M_MOV);

	if (r)
	{
		if (count == 0)
		{
			float dx = timeX - T.x;
			float dy = timeY - T.y;
			b2Vec2 vec(dx,dy);

			SetDirection(vec);
			count++;
		}

		float Dx = timeX - T.x;
		float Dy = timeY - T.y;

		if ( Dx < 0)
			Dx = -Dx;
		if ( Dy < 0)
			Dy = -Dy;

		if (Dx < 5 && Dy <5)
		{
			SetState(0, S_M_MOV);
			count = 0;
		}
	}
}


void Scout::processInput()
{
	hge->Input_GetMousePos(&lastX, &lastY);
	lastX += T.x - 400;
	lastY += T.y - 300;
	while (hge->Input_GetEvent(inputEvent))
	{
		switch (inputEvent->key)
		{
		case HGEK_LBUTTON:
			if (inputEvent->type == INPUT_MBUTTONDOWN) 	type = 1; 
			else 
			{
				if (type == 1)
				{
					timeX = lastX;
					timeY = lastY;
					SetState(S_M_MOV, S_M_MOV);
					type = 0;
					count = 0;
				}
				else
					SetState(0, S_M_MOV);
			}
			break;
		case HGEK_RBUTTON:
			if (Shield::available(this)) {
				if (inputEvent->type == INPUT_MBUTTONDOWN) SetState(M_SHIELD, M_SHIELD);
				else SetState(0, M_SHIELD);
			}
			break;
		case HGEK_B:
			if (inputEvent->type == INPUT_KEYDOWN) SetState(S_M_BUR, S_M_BUR);
			else SetState(0,S_M_BUR);
			break;
		case HGEK_W:
			if (inputEvent->type == INPUT_KEYDOWN) SetState(S_M_WIN, S_M_WIN);
			else SetState(0,S_M_WIN);
			break;
		}
	}
}